/*
 * Copyright 2003,2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.jakubiak.jpa.translator;

import eu.jakubiak.jpa.translator.locale.LocaleParser;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 *
 * @author jakubiak_a
 */
public class TranslatorProperties {

    public static final String BUNDLE_NAME = "jpatranslator";
    public static final String KEY_PERSISTENCE_UNIT_NAME = "persistenceUnitName";
    public static final String KEY_DEFAULT_JPA_LOCALE = "defaultJpaLocale";
    private static final Properties properties;

    /**
     * Loading properties from properties file
     */
    static {
        properties = new Properties();
        ResourceBundle rb = ResourceBundle.getBundle(BUNDLE_NAME);
        for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
            final String key = (String) keys.nextElement();
            final String value = rb.getString(key);
            properties.put(key, value);
        }
        
        
        // get default locale
        defaultLocale = Locale.getDefault();
        String localeString = properties.getProperty(KEY_DEFAULT_JPA_LOCALE, null);
        if (localeString != null) {
            LocaleParser lp = new LocaleParser(localeString);
            if(null!=lp.getLocale()) {
                defaultLocale = lp.getLocale();
            }
        }

    }

    public static String getPersistenceUnitName() {
        return properties.getProperty(KEY_PERSISTENCE_UNIT_NAME, "translatorPU");
    }
    /**
     * Lazy load
     */
    public static Locale defaultLocale;

    /**
     * Default database locale
     * 
     * http://en.wikipedia.org/wiki/Locale
     * [language[_territory][.codeset][@modifier]]
     * @return
     */
    public static Locale getDefaultJpaLocale() {
        return defaultLocale;
    }
}
